;; 基础优化设置
;; 好用的基础包

;;
(setq gc-cons-threshold (* 32 1024 1024))
;; 编码设置
(set-language-environment "UTF-8")
(prefer-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(if (boundp 'buffer-file-coding-system)
    (setq-default buffer-file-coding-system 'utf-8)
  (setq default-buffer-file-coding-system 'utf-8))


;; 删除选取内容
(delete-selection-mode t)

;; Tab 设置
(setq default-tab-width 4)
(setq-local tab-width 4)
(setq-default tab-width 4)
(setq c-basic-offset 4)
;; 不备份文件，不自动保存
(setq make-backup-files nil)
(setq-default make-default nil)
(setq auto-save-default nil)
(setq-default auto-save-default nil)

;; 最近打开文件
(require 'recentf)
(recentf-mode 1)
(setq recentf-max-menu-items 60)

;; dired 设置
(setq dired-recursive-deletes 'always)
(setq dired-recursive-copies 'always)
(put 'dired-find-alternate-file 'disabled nil)

(with-eval-after-load 'dired
  (define-key dired-mode-map (kbd "RET") 'dired-find-alternate-file))

;; 自动刷新被修改过的文件
(global-auto-revert-mode 1)

;; 关闭锁文件
(setq create-lockfiles nil)

;; 总是加载最新的文件

(setq load-prefer-newer t)

;; 关闭字体缓存gc
(setq inhibit-compacting-font-caches nil)
(setq package-check-signature nil)
;ooGNU ELPA	http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/
;NonGNU ELPA	http://mirrors.tuna.tsinghua.edu.cn/elpa/nongnu/
;GNU ELPA Devel	http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu-devel/
;NonGNU ELPA Devel	http://mirrors.tuna.tsinghua.edu.cn/elpa/nongnu-devel/
;MELPA	http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/
;MELPA Stable	http://mirrors.tuna.tsinghua.edu.cn/elpa/stable-melpa/
;Org	http://mirrors.tuna.tsinghua.edu.cn/elpa/org/
(setq package-archives '(("gnu" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")						 
						("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))


(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))      

(require 'use-package)
(setq use-package-always-ensure t)

(use-package exec-path-from-shell
  :config
  (exec-path-from-shell-initialize))

(use-package winum
             :init
             (winum-mode))


(use-package crux
  :bind (("C-a" . crux-move-beginning-of-line)
	 ("C-c ^" . crux-top-join-line)
	 ("C-x ," . crux-find-use-init-file)
	 ("C-S-k" . crux-smart-kill-line)))

(use-package smartparens
  :init
  (require 'smartparens-config)
  :config
  (smartparens-global-mode t))

(use-package vertico
  :init
  (vertico-mode))

(use-package orderless
  :init
  (setq completion-styles '(orderless basic)
	completion-category-defaults nil
	completion-category-overrides '((file (styles partial-completion)))))

(use-package marginalia)
(use-package embark
  :config
  (setq prefix-help-command 'embark-prefix-help-command)
  :bind
  (("C-;" . embark-act)))

(use-package consult
  :bind
  (("C-s" . consult-line)))

(define-key vertico-map "?" #'minibuffer-completion-help)
(define-key vertico-map (kbd "M-RET") #'minibuffer-force-complete-and-exit)
(define-key vertico-map (kbd "M-TAB") #'minibuffer-complete)


(setq-default cursor-type 'bar)
(global-hl-line-mode t)
(setq ring-bell-function 'ignore blink-cursor-mode nil)
(scroll-bar-mode -1)
(tool-bar-mode -1)

(setq scroll-step 2
      scroll-margin 2
      hscroll-step 2
      hscroll-margin 2
      scroll-conservatively 101
      scroll-up-aggressively 0.01
      scroll-down-aggressively 0.01
      scroll-preserve-screen-position 'always)

(setq auto-window-vscroll nil)
(setq display-line-numbers-type 'relative)
(global-display-line-numbers-mode t)

(use-package hungry-delete
  :config
  (setq hungry-delete-join-reluctantly t)
  :hook
  ((after-init . global-hungry-delete-mode)))
(use-package neotree
  :config
  (global-set-key [f8] 'neotree-toggle)
  (setq neo-theme 'icons))

(use-package all-the-icons)

(use-package which-key :config (which-key-mode))


;; keybind

(setq w32-apps-modifier 'super)
(setq mac-option-modifier 'meta
      mac-command-modifier 'super)

(global-set-key (kbd "s-a") 'mark-whole-buffer)
(global-set-key (kbd "s-c") 'kill-ring-save)
(global-set-key (kbd "s-s") 'save-buffer)
(global-set-key (kbd "s-v") 'yank)
(global-set-key (kbd "s-z") 'undo)
(global-set-key (kbd "C-q") 'keyboard-quit)
(global-set-key (kbd "s-x") 'kill-region)

(use-package evil
             :init
             (evil-mode 1)
             :config
             (setcdr evil-insert-state-map nil)
             (define-key evil-insert-state-map [escape] 'evil-normal-state))

(use-package evil-leader
             :config
             (evil-leader/set-leader "<SPC>")
             (evil-leader/set-key
             "1" 'winum-select-window-1
             "2" 'winum-select-window-2
             "3" 'winum-select-window-3
             "4" 'winum-select-window-4
             "5" 'winum-select-window-5
			 "ff" 'find-file
			 "fr" 'save-buffer
             "wo" 'delete-other-windows
             "wd" 'delete-window
             "w\\" 'split-window-horizontally
             "w-" 'split-window-below
             "bb" 'switch-to-buffer
	     "bd" 'kill-buffer)
:init
(global-evil-leader-mode))


(provide 'better)

